package com.dq.Controller;

import com.dq.Pojo.Homework;
import com.dq.Pojo.Result;
import com.dq.Service.HomeworkService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/homework")
public class HomeController {
    @Autowired
    private HomeworkService homeworkService;
    @PostMapping("/addORupdate")
    public Result addORupdate(@RequestBody Homework homework){
        if (homework.getHomeworkid()==null){
            homeworkService.add(homework);
        }else{
            homeworkService.update(homework);
        }
        return Result.success();
    }
    @GetMapping("/findAll")
    public Result findAll(@RequestParam Integer pageNum,
                          @RequestParam Integer pageSize,
                          @RequestParam Integer courseId,
                          @RequestParam Integer classId,
                          @RequestParam String name,
                          @RequestParam String type){
        PageInfo<Homework> homeworkPageInfo=homeworkService.findAll(pageNum,pageSize,classId,courseId,name,type);
        return Result.success(homeworkPageInfo);
    }
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer id){
        homeworkService.delete(id);
        return  Result.success();
    }

    @GetMapping("/findByid")
    public Result findByid(@RequestParam Integer courseid,@RequestParam Integer classid){
        List<Homework> homeworkList =homeworkService.findByid(classid,courseid);
        return  Result.success(homeworkList);
    }

    @GetMapping("/getByid")
    public Result getByid(@RequestParam Integer id){
        Homework homework=homeworkService.getByid(id);
        return Result.success(homework);
    }



}
